//
//  HEOrgPhotoCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/26.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEOrgPhotoCell.h"
#import "HEOldPelOrgModel.h"

@interface HEOrgPhotoCell ()

@property(nonatomic, strong) UILabel *titleLb;
@property(nonatomic, strong) NSMutableArray *btnArr;

@end

@implementation HEOrgPhotoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = KWhiteColor;
        
        _btnArr = [NSMutableArray array];

        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    UIView *dotView = [UIView new];
    dotView.backgroundColor = KHexColor(@"FFA900");
    [self.contentView addSubview:dotView];

    [dotView hg_setAllCornerWithCornerRadius: 5];
    
    _titleLb = [UILabel new];
    _titleLb.font = DYBlodFont(18);
    _titleLb.text = @"-";
    _titleLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview:_titleLb];
    
    [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_titleLb);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.width.mas_offset(10);
        make.height.mas_offset(10);
    }];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(dotView.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
    [_titleLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
   
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
 
    _titleLb.text = fiterModel.leftTitle;
}

- (void)configCellWithModel:(HEOldPelOrgModel *)model {
 
    if (_btnArr.count == 0) {
        
        //存放上一个button
        UIImageView *buttonBefore;
        //存放button距离左侧的宽度，来确定是否需要换行
        __block CGFloat right = 0.0;
        //间距
        CGFloat margin = 15.f;
        for(int i = 0; i < model.KPhotoArr.count; i++) {
           
            UIImageView *backBtn = [UIImageView new];
            [backBtn sd_setImageWithURL:[NSURL URLWithString:model.KPhotoArr[i]] placeholderImage:IMAGE_NAMED(@"noImage")];
            [self.contentView addSubview:backBtn];
            [_btnArr addObject:backBtn];
            
            CGFloat fitWidth = (KScreenWidth - 30 - 30) / 2.0;

            [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(fitWidth);
                make.height.mas_equalTo(160);
                if(i==0){
                  make.left.equalTo(self.contentView).offset(margin);
                  make.top.equalTo(_titleLb.mas_bottom).offset(10);
                  right = margin + fitWidth + margin;
                }else{
                  if(right + fitWidth > KScreenWidth){
                      make.top.mas_equalTo(buttonBefore.mas_bottom).offset(10);
                      make.left.equalTo(self.contentView).offset(margin);
                      right =  margin+fitWidth+margin;
                  }else{
                      make.left.mas_equalTo(buttonBefore.mas_right).offset(margin);
                      make.top.equalTo(buttonBefore);
                      right += fitWidth+margin;
                  }
                }
                
                if (i == (model.KPhotoArr.count -1)) {
                    make.bottom.equalTo(self.contentView).mas_offset(-10);
                }
            }];
            buttonBefore = backBtn;
        }
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
